package com.objectcarpentry.kaylee.ui.handlers;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import com.objectcarpentry.kaylee.KayleeFactory;
import com.objectcarpentry.kaylee.KayleePlugin;
import com.objectcarpentry.kaylee.Library;
import com.objectcarpentry.kaylee.Track;
import com.objectcarpentry.kaylee.gstreamer.jobs.MetadataJob;

public class AddFoldersHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
		DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN | SWT.MULTI);
		dialog.setMessage("Select directories whose contents will be added to your library");

		String dirName = System.getProperty("user.home");
		File path = new File(dirName);
		dialog.setFilterPath((new Path(path.getAbsolutePath())).toOSString());
					
		String selectedDirectory = dialog.open();
					
				
		if (selectedDirectory != null){
		
			KayleePlugin core = KayleePlugin.getDefault();
			
			final Library library = core.getLibrary();
			final Collection<Track> tracks = addTracks(new File(selectedDirectory), null, null);
			
			
			MetadataJob job = new MetadataJob("Metadata tatata", tracks);
			job.addJobChangeListener(new JobChangeAdapter(){
				@Override
				public void done(IJobChangeEvent event) {
					PlatformUI.getWorkbench().getDisplay().asyncExec(
						new Runnable(){
							public void run(){
								KayleePlugin core = KayleePlugin.getDefault();
								Command c = AddCommand.create(core.getEditingDomain(), library, null, tracks);
								core.getEditingDomain().getCommandStack().execute(c);
								try {
									library.eResource().save(Collections.EMPTY_MAP);
								} catch (IOException e){
									e.printStackTrace();
								}
							}
						}
					);
				}
			});
			
			PlatformUI.getWorkbench().getProgressService().showInDialog(null, job);
			job.schedule();
			
		}
		return null;
	}

	protected Collection<Track> addTracks(File directory, Collection<Track> tracks, FileFilter filter){
		if (tracks == null ) {
			tracks = new ArrayList<Track>();
		}
		
		if (filter == null) {
			filter = new FileFilter(){
				@Override
				public boolean accept(File pathname) {
					return ((pathname.isFile() && pathname.getName().endsWith(".mp3") || pathname.isDirectory()));
				}
			};
		}
		
		if (directory.isDirectory()){
			File[] files = directory.listFiles(filter);
			for (File f : files) {
				if (f.isFile()) {
					Track t = KayleeFactory.eINSTANCE.createTrack();
					t.setLocation(URI.createFileURI(f.getAbsolutePath()));
					tracks.add(t);
				} else {
					addTracks(f, tracks, filter);
				}
			}
		}
		
		return tracks;
	}
	
}
